package com.foreveross.zillasdk;

import android.app.Dialog;
import android.content.Context;

import com.darin.cl.http.CLHttpUtil;
import com.darin.cl.task.CLTask;
import com.foreveross.util.DialogUtils;

/**
 * 处理ZillaManager上各种请求封装类
 * 
 * @author Jianfeng.Lao
 * 
 */
public abstract class ZillaRequestTask extends CLTask<Object, Object, String> {

	private Dialog mProgressDialog;
	private Dialog mErrorDialog;
	private boolean mShowProgressDialog;
	private boolean mShowErrorDialog;
	private boolean mCancelable = false;
	protected Context mContext;
	protected String progressContent = "请稍候...";
	private ZillaListener mListener;
	protected CLHttpUtil mHttpClient;

	public ZillaRequestTask(Context mContext,ZillaListener listener) {
		super();
		this.mContext = mContext;
		this.mListener=listener;
	}

	public void setShowProgressDialog(boolean mShowProgressDialog) {
		this.mShowProgressDialog = mShowProgressDialog;
	}

	public void setShowErrorDialog(boolean mShowErrorDialog) {
		this.mShowErrorDialog = mShowErrorDialog;
	}

	protected void showProgressDialog() {
		dismissProgressDialog();
		if (mShowProgressDialog) {
			mProgressDialog = DialogUtils.showloadingDialog(mContext, progressContent);
			mProgressDialog.setCancelable(mCancelable);
			mProgressDialog.show();
		}

	}

	protected void dismissProgressDialog() {
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
			mProgressDialog = null;
		}
	}

	protected void showErrorDialog() {
		dismissProgressDialog();
		dismissErrorDialog();
		if (mShowErrorDialog) {
			mErrorDialog = DialogUtils.getAlertDialog(mContext, null, "请求错误", "确定", null, null,
					null);
			mErrorDialog.show();
		}
	}

	protected void dismissErrorDialog() {
		if (mErrorDialog != null && mErrorDialog.isShowing()) {
			mErrorDialog.dismiss();
			mErrorDialog = null;
		}
	}

	public void setCancelable(boolean mCancelable) {
		this.mCancelable = mCancelable;
	}

	public void setDialogContent(String dialogContent) {
		this.progressContent = dialogContent;
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		showProgressDialog();
		if(mListener!=null){
			mListener.requestStart();
		}
	}
	
	@Override
	public void doOnSuccess(String result) {
		if(mListener!=null){
			mListener.requestSuccess(result);
		}
	}


	@Override
	protected void doOnException(Exception exception) {
		super.doOnException(exception);
		showErrorDialog();
		if(mListener!=null){
			mListener.requestFailed(exception);
		}
	}

	@Override
	protected boolean handlerException(Exception exception) {
		if (exception == null){
			return false;
		}
		return true;
	}

	@Override
	protected void onFinish() {
		super.onFinish();
		if(mHttpClient!=null){
			mHttpClient.shutdown();
		}
		if(mErrorDialog!=null&&mErrorDialog.isShowing()){
			//task 出现exception并显现了error dialog
		}else{
			dismissProgressDialog();
		}
	}

	@Override
	protected void beforeCancel() {
		super.beforeCancel();
		if(mHttpClient!=null){
			mHttpClient.shutdown();
		}
	}
}
